<template>
  <div>
    <el-tag
      :key="url"
      v-for="url in whiteList"
      closable
      :disable-transitions="false"
      @close="handleClose(url)">
      {{url}}
    </el-tag>
    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">添加白名单</el-button>
  </div>
</template>

<script>
import { listWhiteList, delWhiteList, addWhiteList } from "@/api/whitelist/whitelist"

export default {
  name: "WhiteList",
  data() {
    return {
      // 前台用户管理表格数据
      whiteList: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询前台用户管理列表 */
    getList() {
      this.loading = true
      listWhiteList().then(response => {
        console.log('whitelist',response)
        this.whiteList = response
      })
    },
    handleClose(tag) {
      delWhiteList(tag).then(response => {
        console.log('del',response)
        this.getList()
      })
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        addWhiteList(inputValue).then(response => {
          console.log('add',response)
          this.getList()
        })
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>
<style>
.el-tag + .el-tag {
  margin: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
